﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using WCYCReader.Main;
using WCYCReader.StorageHelper;

namespace WCYCReader.Pages
{
    public partial class CacheClearPage : PhoneApplicationPage
    {
        public CacheClearPage()
        {
            InitializeComponent();
        }

        private void AppBarScanCache_Click(object sender, EventArgs e)
        {
//Local/Shared/ShellContent
//Local/Shared/Media
//Local/Shared/Transfers
//Local/content
//Local

            //var directories = IsolatedStorageHelper.Instance.GetDirectories("Local");
            string[] files12= StorageHelper.IsolatedStorageHelper.Instance.GetFiles("content");
            //this.llsCache.ItemsSource = new ObservableCollection<string>(files);


            var storageFile = IsolatedStorageFile.GetUserStoreForApplication();
            var dires = storageFile.GetDirectoryNames();
            var files1 = storageFile.GetFileNames();

            //WCYCReader.Utility.StorageFileHelper sfh = new Utility.StorageFileHelper("/Shared/ShellContent/");
            //string appDir = sfh.GetAppDir();
            //string bkp = sfh.GetBKPictureDir();
            
            //string gtd = sfh.GetTempDir();
            //var fns = sfh.StorageFile.GetFileNames();

            try
            {
                var settings = IsolatedStorageSettings.ApplicationSettings;
                foreach (string keypair in settings.Keys)//遍历键值对集合中的键，并根据键名从键值对集合中清除此键
                {
                    if (settings.Contains(keypair))
                    {
                        //settings.Remove(keypair);
                        LogHelper.LogInfo(string.Concat(keypair,":\t",settings[keypair].ToString()),this.GetType(), MethodBase.GetCurrentMethod());
                    }
                }
                //settings.Save();
                
            }
            catch
            { }

            try
            {
                using (var isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    var rootfiles = storageFile.GetFileNames();
                    if (rootfiles != null && rootfiles.Length > 0)
                    {
                        //这是远程配置缓存文件
                        rootfiles = (from p in rootfiles where !p.Equals("__ApplicationSettings") select p).ToArray<string>();
                        if (rootfiles != null && rootfiles.Length > 0)
                        {
                            foreach (string rootfile in rootfiles)
                            {
                                LogHelper.LogInfo(string.Concat("FileExists:\t:", rootfile));
                                if (isolatedStorageFile.FileExists(rootfile))
                                {
                                    LogHelper.LogInfo(string.Concat("FileExists:\t:", true));
                                }
                            }
                        }
                    }
                    

                    //指定目录下的子目录
                    string[] directories = isolatedStorageFile.GetDirectoryNames();
                    if (directories != null && directories.Length > 0)
                    {
                        foreach (string directory in directories)
                        {
                            if (!"content".Equals(directory))
                                continue;
                            LogHelper.LogInfo(string.Concat("Directory:\t:", directory));                            
                            var files = isolatedStorageFile.GetFileNames(Path.Combine("content", "*"));
                            if (files != null && files.Length > 0)
                            {
                                foreach (string file in files)
                                {
                                    LogHelper.LogInfo(string.Concat("\tFile:\t:", file));
                                }
                            }
                            else
                            {
                                string[] subDirectories = isolatedStorageFile.GetDirectoryNames(Path.Combine(directory, "*"));
                                if (subDirectories != null && subDirectories.Length > 0)
                                {
                                    foreach (string subDirectory in subDirectories)
                                    {
                                        LogHelper.LogInfo(string.Concat("SubDirectory:\t:", subDirectory));
                                        if ("Media".Equals(subDirectory) || "ShellContent".Equals(subDirectory) || "Transfers".Equals(subDirectory))
                                        {
                                            continue;
                                        }
                                        else
                                        { 
                                        }
                                        var subFiles = isolatedStorageFile.GetFileNames(Path.Combine(subDirectory, "*"));
                                        if (subFiles != null && subFiles.Length > 0)
                                        {
                                            foreach (string subFile in subFiles)
                                            {
                                                LogHelper.LogInfo(string.Concat("\tSubFile:\t:", subFile));
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                //    //指定目录下的文件列表
                //    var files = isolatedStorageFile.GetFileNames(Path.Combine(Keys.MediabrisCacheDirectory, "*"));
                //    foreach (string filename in files)
                //    {
                //        string filepath = Path.Combine(Keys.MediabrisCacheDirectory, filename);
                //        if (isolatedStorageFile.FileExists(filepath))
                //        {
                //            isolatedStorageFile.DeleteFile(filepath);
                //        }
                //    }
                //    isolatedStorageFile.DeleteDirectory(Keys.MediabrisCacheDirectory);
                //}
                //isolatedStorageFile.Dispose();
            }
            catch(Exception err)
            {
                LogHelper.LogError(err.ToString());
            }
            
        }

        private void AppBarCacheClear_Click(object sender, EventArgs e)
        {

        }
    }
}